package sipus.database.hibernate;

import java.io.Serializable;

import org.hibernate.HibernateException;
import org.hibernate.event.DeleteEvent;
import org.hibernate.event.def.DefaultDeleteEventListener;

import sipus.common.CommonPrivilages;
import sipus.database.model.AccessedUsers;
import sipus.database.model.DetailLogLogin;
import sipus.database.model.LogUserActifity;


public class DeleteEventListener extends DefaultDeleteEventListener {
	/**
	 * 
	 */
	private static final long serialVersionUID = 8669099681437568970L;

	@SuppressWarnings("rawtypes")
	@Override
	public void onDelete(DeleteEvent event) throws HibernateException {
		super.onDelete(event);
		try {
			Serializable serializable = (Serializable) event.getObject();
			if (serializable != null
					&& !(serializable instanceof AccessedUsers)
					&& !(serializable instanceof DetailLogLogin)
					&& !(serializable instanceof LogUserActifity)) {
				Class myClass = null;
				try {
					throw new Exception("Who called me?");
				} catch (Exception e) {
					try {
						StackTraceElement[] elements = e.getStackTrace();
						for (StackTraceElement element : elements) {
							if (element.getClassName().startsWith(
									"sipus.action.master")) {
								myClass = Class.forName(element.getClassName());
								break;
							}
						}
					} catch (Exception e1) {
						e1.printStackTrace();
					}
				}
				if (myClass != null) {
					System.out.println("============= delete for "
							+ serializable.getClass().getName()
							+ ", this method called from " + myClass.getName()
							+ " ===================");
					CommonPrivilages.saveActivity(myClass,
							CommonPrivilages.DELETE, serializable, "");
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}

	}

}
